import { Injectable, UnauthorizedException } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { UsersService } from 'src/users/users.service';

@Injectable()
export class AuthService {
  constructor(
    private usersService: UsersService,
    private jwtService: JwtService,
  ) {}

  async signIn(username, pass) {
    const user = await this.usersService.user({ username });
    console.log(user, 'user');
    if (!user) {
      throw new UnauthorizedException('没有该用户');
    }
    if (user?.password !== pass) {
      throw new UnauthorizedException('密码不正确');
    }
    const payload = { id: user.id, username: user.username };
    return {
      access_token: await this.jwtService.signAsync(payload),
    };
  }
  async Rregister(data) {
    const user = await this.usersService.createUser(data);
    console.log('user', user);
  }
}
